package com.mediacloud.app.newsmodule.fragment.navlive

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapterX
import com.utils.read.ReadPlayView4

class NavLiveDropDownListAdapter(context: Context) : BaseRecyclerAdapterX<ArticleItem>(context) {
    var selectedIndex: Int = 0
        set(value) {
            field = value
            if (data!!.size > 0)
                notifyDataSetChanged()
        }

    var channelPlayBills: MutableMap<String, MutableList<XPlayBill>?> = HashMap()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return NavLiveChannelDropDownItem(LayoutInflater.from(context).inflate(R.layout.navchanneldropdownitem, parent, false))
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        if (holder is NavLiveChannelDropDownItem) {
            val item = getItem(position)
            var bill: XPlayBill? = null
            val channelid = item.vid
            channelid?.let {
                val playbills = channelPlayBills[it]
                if (playbills != null) {
                    val size = playbills.size
                    for (i in 0 until size) {
                        val tempBill = playbills[i]
                        val currentTime = System.currentTimeMillis()
                        if (tempBill.startStamp < currentTime && tempBill.endStamp > currentTime) {
                            tempBill.max = (tempBill.endStamp - tempBill.startStamp).toInt()
                            tempBill.progress = (currentTime - tempBill.startStamp).toInt()
                            bill = tempBill
                            break
                        }
                    }
                }
            }
            if (bill == null) {
                holder.programTv.visibility = View.GONE
                holder.startTime.visibility = View.GONE
                holder.endTime.visibility = View.GONE
                holder.programProgress.visibility = View.GONE
            } else {
                holder.programTv.visibility = View.VISIBLE
                holder.startTime.visibility = View.VISIBLE
                holder.endTime.visibility = View.VISIBLE
                holder.programProgress.visibility = View.VISIBLE
                holder.programTv.text = bill!!.Name
                holder.startTime.text = bill!!.start_format
                holder.endTime.text = bill!!.end_format
                holder.programProgress.max = bill!!.max
                holder.programProgress.progress = bill!!.progress
            }

            holder.logoIv.loadX(item.logo, 2, 0, false)
            holder.channelName.text = item.title
            if (selectedIndex == position) {
                holder.readView.visibility = View.VISIBLE
                holder.readView.start()
            } else {
                holder.readView.visibility = View.GONE
                holder.readView.pause()
            }
        }
    }

    class NavLiveChannelDropDownItem(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {

        val channelName: TextView = itemView.findViewById(R.id.channelName)
        val logoIv: ImageView = itemView.findViewById(R.id.logoIv)
        val readView: ReadPlayView4 = itemView.findViewById(R.id.readView)
        val programTv: TextView = itemView.findViewById(R.id.programTv)
        val startTime: TextView = itemView.findViewById(R.id.startTime)
        val endTime: TextView = itemView.findViewById(R.id.endTime)
        val programProgress: ProgressBar = itemView.findViewById(R.id.programProgress)
    }
}